package com.mhz.db;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

public class MyDb {
	
	public static void main(String[] args) throws Exception{
		//MyDb.createDataBase("my-mongodb.xml", "mydb");
		Emp emp=new Emp();
		emp.setAge(18);
		emp.setName("mhz");
		emp.setSex("男");
		//MyDb.insertData("my-mongodb.xml", "mydb",emp);
		//emp.set_uuid(uuid);
		//emp.setName("ll");
		//MyDb.updateData("my-mongodb.xml", "mydb",emp);
		//MyDb.findData("my-mongodb.xml", "mydb");
		emp.set_uuid("d22f1f74-7008-4ddf-a6bf-abcb6f78dc65");
		MyDb.delete("my-mongodb.xml", "mydb",emp);
	}
	
	private static final String _uuid = "_uuid";//主键生成方式
	private static final String uuid  = "3a53763a-6ee4-45e1-8c47-42eb9ddc9dda";//临时数据标uuid
	/**
	 * 创建数据库
	 * @param path
	 * @param dbName
	 * @throws IOException 
	 */
	public static void createDataBase(String path,String dbName) throws IOException{
		FileOutputStream file = new FileOutputStream(path);
		Document document = new Document();
		Element root = new Element("database");
		Element e1 = new Element("table");
		if("".equals(dbName)){
			e1.setAttribute("name", "test");
		}else{
			e1.setAttribute("name", dbName);
		}
		Element e2 = new Element("table");
		e2.setAttribute("name", "system.indexs");
		Element e3 = new Element("table");
		e3.setAttribute("name", "system.users");

		root.addContent(e1);
		root.addContent(e2);
		root.addContent(e3);

		document.setRootElement(root);
		XMLOutputter out = new XMLOutputter();
		out.output(document, file);
		System.out.println("----------create---database---success-------------------");
		
	}
	/**
	 * 插入数据
	 * @param path
	 * @param dbName
	 * @param obj
	 */
	public static Boolean insertData(String path,String dbName,Object entity) throws Exception{
		FileInputStream file = new FileInputStream(path);
		SAXBuilder saxBuilder = new SAXBuilder();
		Document document = saxBuilder.build(file);
		Element root = document.getRootElement();
		
		boolean flag = false;
		List<Element> list = root.getChildren();
		for (Element x : list) {
			if (x.getAttributeValue("name").equals(dbName)) {
				Map base = new HashMap();
				base.put(_uuid, UUID.randomUUID().toString());
				Element data = new Element("data");
				String json = JSON.toJSONString(entity);
				Map mp = JSON.parseObject(json, Map.class);
				base.putAll(mp);
				data.addContent(JSON.toJSONString(base));
				x.addContent(data);
			}
		}
		XMLOutputter out = new XMLOutputter();
		out.output(document, new FileOutputStream(path));
		flag = true;
		System.out
				.println("----------insert --- data --- success----------------------");
		return flag;
	}
	/**
	 * 更新数据
	 * @param path
	 * @param dbName
	 * @param entity
	 * @return
	 * @throws Exception
	 */
	public static Boolean updateData(String path,String dbName,Object entity)throws Exception{
		boolean flag = false;
		FileInputStream file = new FileInputStream(path);
		SAXBuilder saxBuilder = new SAXBuilder();
		Document document = saxBuilder.build(file);
		Element root = document.getRootElement();
		
		List<Element> list = root.getChildren();
		for (Element x : list) {
			if (x.getAttributeValue("name").equals(dbName)) {
				List<Element> dataE=x.getChildren();
				for(Element y : dataE){
					Map old = JSON.parseObject(y.getText(),Map.class);
					String jsonN=JSON.toJSONString(entity);
					Map newE = JSON.parseObject(jsonN, Map.class);
					if(old.get("_uuid").equals(newE.get("_uuid"))){
						old.putAll(newE);
						Element data = new Element("data");
						data.setText(JSON.toJSONString(old));
						x.removeContent(y);
						x.addContent(data);
						break;
					}
				}
				
			}
		}
		XMLOutputter out = new XMLOutputter();
		out.output(document, new FileOutputStream(path));
		System.out
				.println("----------update --- data --- success----------------------");
		flag = true;
		return flag;
	}
	public static Boolean delete(String path,String dbName,Object entity)throws Exception{
		Boolean flag=false;
		FileInputStream file = new FileInputStream(path);
		SAXBuilder saxBuilder = new SAXBuilder();
		Document document = saxBuilder.build(file);
		Element root = document.getRootElement();
		
		List<Element> list = root.getChildren();
		for (Element x : list) {
			if (x.getAttributeValue("name").equals(dbName)) {
				List<Element> dataE=x.getChildren();
				for(Element y : dataE){
					Map old = JSON.parseObject(y.getText(),Map.class);
					String jsonN=JSON.toJSONString(entity);
					Map newE = JSON.parseObject(jsonN, Map.class);
					if(old.get("_uuid").equals(newE.get("uuid"))){
						x.removeContent(y);
						break;
					}
				}
				
			}
		}
		XMLOutputter out = new XMLOutputter();
		out.output(document, new FileOutputStream(path));
		System.out
				.println("----------delete--- data --- success----------------------");
		flag=true;
		return flag;
	}
	/**
	 * 数据查询
	 * @param path
	 * @param dbName
	 * @return
	 * @throws Exception
	 */
	public static String findData(String path,String dbName)throws Exception{
		String result="";
		FileInputStream file = new FileInputStream(path);
		SAXBuilder saxBuilder = new SAXBuilder();
		Document document = saxBuilder.build(file);
		Element root = document.getRootElement();
		
		List<Element> list = root.getChildren();
		JSONArray ary=new JSONArray();
		for (Element x : list) {
			if (x.getAttributeValue("name").equals(dbName)) {
				List<Element> dataE=x.getChildren();
				for(Element y : dataE){
					JSON obj=JSON.parseObject(y.getText(),JSON.class);
					ary.add(obj);
				}
				
			}
		}
		System.out.println("----------data----------------------"+JSON.toJSONString(ary));
		return result;
	}
}
